// pages/home/postDetail/postDetail.js
import {
  http
} from '../../../utils/apiutils'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    jobInfo: {},
    factoryId: '',
    jobId: '',
    allNum: '',
    allAmount: ''

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      factoryId: options.fid,
      jobId: options.jid
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getJobDetail(this.data.jobId)

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 获取岗位详情
  getJobDetail(id) {
    var that = this
    http.getJobData(id, res => {
      // console.log('成功', res)
      if (res.code == 0) {
        that.setData({
          jobInfo: res.data
        })
        if (res.data.thirdType == 2) {
          that.getAllNumber({ jobId: that.data.jobId })
        }
      } else {
        wx.showToast({
          title: res.msg || res.message,
          icon: 'none'
        })
      }
      wx.setNavigationBarTitle({
        title: that.data.jobInfo.name
      })
    }, err => { })
  },
  //根据岗位ID 统计总金额与总重量
  getAllNumber(id) {
    http.getAllNumber(id, res => {
      if (res.status == 200) {
        if (res.obj) {
          this.setData({
            allNum: res.obj.num,
            allAmount: res.obj.amount
          })
        }
      } else {
        wx.showToast({
          title: res.msg || res.message,
          icon: 'none'
        })
      }
    }, err => { })
  },
  clickEdit() {
    wx.navigateTo({
      url: '/pages/home/editPost/editPost?jobId=' + this.data.jobId + '&&factoryId=' + this.data.factoryId,
    })
  },
  toFx() {
    wx.navigateTo({
      url: '/pages/home/assessment/index?id=' + this.data.jobInfo.id + '&&jobName=' + this.data.jobInfo.name,
    })
  },
  clickPhone(e) {
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.key,
    })
  },
  toJobDtl() {
    wx.navigateToMiniProgram({
      appId: 'wx866b2c455133dce9',
      path: '/pages/index/index?type=work&id=' + this.data.jobId
    })
  },
  toUnit() {
    wx.navigateTo({
      url: '/pages/home/unit/unit?id='+this.data.jobId+'&&type='+this.data.jobInfo.unitType,
    })
  },
})